﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using System.Security.Permissions;

namespace DataAccess {
    public class PartnerRepository : DataProvider {
        public int register() {
            return 1;
        }

        [SecurityPermission(SecurityAction.Demand, ControlDomainPolicy = true)]
        public DataTable isPartner(String loginName, String pwd) {
            MD5 hash = MD5.Create();
            String sql = String.Format("SELECT * FROM Partners WHERE PartnerLoginName = '{0}' AND PartnerPwd = '{1}'", loginName, pwd);
            return this.executeQueryDataTable(sql);
        }

        /// <summary>
        /// get partner list
        /// </summary>
        /// <returns></returns>
        public DataTable getPartnerList() {
            DataTable table = new DataTable();
            String sql = "SELECT * FROM Partners";
            table = this.executeQueryDataTable(sql);
            return table;
        }

        public DataTable getPartner(int partnerID) {
            DataTable table = new DataTable();
            String sql = "SELECT * FROM Partners WHERE PartnerID = " + partnerID;
            table = this.executeQueryDataTable(sql);
            return table;
        }

        public bool update(int partnerID, String partnerName, String partnerLogin, String partnerPwd, String wsName, String wsLink, String pvdID, String pvdPwd) {
            String sql = "UPDATE Partners SET PartnerLoginName = '{0}', PartnerPwd = '{1}', Name = '{2}', WS_Name = '{3}', LinkWS_RefuseTrans = '{4}', LinkWS_AcceptTrans = '{5}', LinkWS_CheckInventory = '{6}', LinkWS_UpdateInventory = '{7}', LinkWS_Login = '{8}', PvdID = '{9}', PvdPwd = '{10}' WHERE PartnerID = {11}";
            sql = string.Format(sql, partnerLogin, partnerPwd, partnerName, wsName, wsLink, wsLink, wsLink, wsLink, wsLink, pvdID, pvdPwd, partnerID);

            if (this.executeQuery(sql) == 1)
                return true;

            return false;
        }

        public bool insert(String partnerName, String partnerLogin, String partnerPwd, String wsName, String wsLink, String pvdID, String pvdPwd) {
            string sql = "INSERT INTo Partners(Name, PartnerLoginName, PartnerPwd, WS_Name, LinkWS_RefuseTrans, LinkWS_AcceptTrans, LinkWS_CheckInventory, LinkWS_UpdateInventory, LinkWS_Login, PvdID, PvdPwd) VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}')";

            sql = string.Format(sql, partnerLogin, partnerPwd, partnerName, wsName, wsLink, wsLink, wsLink, wsLink, wsLink, pvdID, pvdPwd);

            if (this.executeQuery(sql) == 1)
                return true;

            return false;
        }
    }
}
